package com.bigstar.lolen_e;

import java.io.File;

import min3d.core.Object3dContainer;
import min3d.core.RendererActivity;
import min3d.parser.ObjParser;
import min3d.vos.Light;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Environment;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.SeekBar;

public class Champion3D extends RendererActivity implements OnTouchListener {

	private Object3dContainer objModel;

	private float touchBeforeX;
	private float touchBeforeY;

	private float touchNowX;
	private float touchNowY;

	private float rotationX;
	private float rotationY;

	private float scale;
	
	
	private float dragTouchBeforeX;
	private float dragTouchBeforeY;

	private float dragTouchNowX;
	private float dragTouchNowY;
	
	private float dragX;
	private float dragY;

	SeekBar mSeekBar;

	Light _lightGreen;

	String resId;

	private ProgressDialog pd;

	@Override
	public void initScene() {
		resId = getIntent().getExtras().getString("champName") + "/"
				+ getIntent().getExtras().getString("skinName") + "/";
		resId = resId.replaceAll(" ", "_");
		runOnUiThread(new Runnable() {
			@Override
			public void run() {
				if (pd == null) {
					pd = ProgressDialog.show(Champion3D.this, "",
							"download Render data...", true);
					pd.show();
				}
			}
		});

		String str = Environment.getExternalStorageState();
		if (str.equals(Environment.MEDIA_MOUNTED)) {
			File dir = new File(com.bigstar.lolen_e.Utils.DATA_PATH + "3d/"
					+ resId);
			if (!dir.exists()) {
				dir.mkdirs();
			}
		}
		
		if(!Utils.loadObject(Champion3D.this, "3d/" + resId, getIntent().getExtras().getString("champName")) 
				|| !Utils.loadMtl(Champion3D.this, "3d/" + resId, getIntent().getExtras().getString("champName"))
				|| !Utils.loadTexture(Champion3D.this, "3d/" + resId, getIntent().getExtras().getString("champName"))){
			runOnUiThread(new Runnable() {
				@Override
				public void run() {
					if (pd != null) {
						pd.dismiss();
						pd = null;
					}
					new AlertDialog.Builder(Champion3D.this)
					.setTitle("LOL")
					.setMessage("Champion of rendering is not yet supported.")
					.setIcon(R.drawable.icon)
					.setPositiveButton("exit", new android.content.DialogInterface.OnClickListener(){

						@Override
						public void onClick(DialogInterface dialog, int which) {
							finish();
							
						}
						
					})
					.show();
				}
			});
			return;
		}

		ObjParser parser = new ObjParser(Champion3D.this, resId, true);
		parser.parse();

		objModel = parser.getParsedObject();
		scale = objModel.scale().x = objModel.scale().y = objModel.scale().z = 0.02f;
		objModel.position().y = -1.4f;
		scene.addChild(objModel);

		scene.lights().add(new Light());
		glSurfaceView().setOnTouchListener(Champion3D.this);
		mSeekBar = (SeekBar) findViewById(R.id.seekbar);
		mSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
			public void onProgressChanged(SeekBar seekBar, int progress,
					boolean fromUser) {
				float temp = 0.02f + ((progress - 50) * 0.0003f);
				scale = objModel.scale().x = objModel.scale().y = objModel
						.scale().z = temp;
				temp = -1.4f - ((progress - 50) * 0.02f);
				objModel.position().y = temp;
			}

			public void onStartTrackingTouch(SeekBar seekBar) {
			}

			public void onStopTrackingTouch(SeekBar seekBar) {
			}
		});

		runOnUiThread(new Runnable() {
			@Override
			public void run() {
				if (pd != null) {
					pd.dismiss();
					pd = null;
				}
			}
		});
	}

	@Override
	public void updateScene() {
		if (objModel != null && objModel.isVisible()) {
			objModel.rotation().x += rotationY;
			objModel.rotation().y += rotationX;
			rotationX = 0;
			rotationY = 0;
			objModel.position().x += dragX;
			objModel.position().y -= dragY;
			dragX = 0;
			dragY = 0;
		}
	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		int act = event.getAction();
		int count = event.getPointerCount();
		if(count <= 1){
			if (event.getAction() == MotionEvent.ACTION_DOWN) {
				touchNowX = event.getX();
				touchNowY = event.getY();
	        	dragTouchNowX = event.getX();
	        	dragTouchNowY = event.getY();
			} else if (event.getAction() == MotionEvent.ACTION_MOVE) {
				touchBeforeX = touchNowX;
				touchBeforeY = touchNowY;
				touchNowX = event.getX();
				touchNowY = event.getY();
				rotationX = (touchNowX - touchBeforeX) / 3;
				rotationY = (touchNowY - touchBeforeY) / 3;
			} else if (event.getAction() == MotionEvent.ACTION_UP) {
				touchNowX = event.getX();
				touchNowY = event.getY();
	        	dragTouchNowX = event.getX();
	        	dragTouchNowY = event.getY();
			}
		}else if(count == 2){
			  switch(act & MotionEvent.ACTION_MASK) {
		        case MotionEvent.ACTION_DOWN:  
		            break;
		        case MotionEvent.ACTION_MOVE: 
		        	dragTouchBeforeX = dragTouchNowX;
		        	dragTouchBeforeY = dragTouchNowY;
		        	dragTouchNowX = event.getX();
		        	dragTouchNowY = event.getY();
		        	dragX = (dragTouchNowX - dragTouchBeforeX) / 250;
		        	dragY = (dragTouchNowY - dragTouchBeforeY) / 250;
		            break;
		        case MotionEvent.ACTION_UP:  
		        case MotionEvent.ACTION_POINTER_UP:
					touchNowX = event.getX();
					touchNowY = event.getY();
		        	dragTouchNowX = event.getX();
		        	dragTouchNowY = event.getY();
		            break;
		        case MotionEvent.ACTION_POINTER_DOWN:  
		            break;
		        case MotionEvent.ACTION_CANCEL:
		        default : 
		            break;
		    }
		}
		// TODO Auto-generated method stub
		return true;
	}
}
